from SegmentTree import SegmentTree
from typing import *


# test
class NumArray:

    def __init__(self, nums: List[int]):
        self.n = len(nums)
        self.st = SegmentTree(nums=nums)

    def update(self, index: int, val: int) -> None:
        self.st.do_update_index(index, val)

    def sumRange(self, left: int, right: int) -> int:
        return self.st.do_query_sum(left, right)


def test(op, params, ans=None):
    na = NumArray(params[0][0])
    ret = []
    for op, param in zip(op, params):
        if op == 'NumArray':
            na = NumArray(params[0][0])
        elif op == 'update':
            na.update(*tuple(param))
        else:
            ret.append(na.sumRange(*tuple(param)))
    print(ret == ans)
    if ret != ans:
        print(ret)


# test 307. 区域和检索 - 数组可修改
# 区域更新
def case_区域更新求和1():
    op = [
        "NumArray", "update", "update", "update", "sumRange", "update",
        "sumRange", "update", "sumRange", "sumRange", "update"
    ]
    params = [[[7, 2, 7, 2, 0]], [4, 6], [0, 2], [0, 9], [4, 4], [3, 8],
              [0, 4], [4, 1], [0, 3], [0, 4], [0, 4]]
    ans = [6, 32, 26, 27]
    test(op, params, ans)


def case_区域更新求和2():
    op = ["NumArray", "update", "sumRange", "sumRange", "update", "sumRange"]
    params = [[[9, -8]], [0, 3], [1, 1], [0, 1], [1, -3], [0, 1]]
    ans = [-8, -5, 0]
    test(op, params, ans)


# test

# leetcode
# [307. 区域和检索 - 数组可修改](https://leetcode.cn/problems/range-sum-query-mutable/)
# leetcode

if __name__ == '__main__':
    case_区域更新求和1()
    case_区域更新求和2()